package simpleftp.server;

import java.net.ServerSocket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Simple serveur TCP creant des thread pour gerer chaque connexion client.
 * 
 * @author lom
 */
public class FTPServerMain
{

	/**
	 * Port d'ecoute.
	 */
	public static final int		PORT	= 7802;
	/**
	 * Socket serveur d'accueil.
	 */
	private static ServerSocket	socketaccueil;

	public static void main(String argv[]) throws Exception
	{
		// TODO MAKE IT MULTITHREAD (about 4 lines)
		try
		{
			socketaccueil = new ServerSocket(PORT);
			System.out.println("Server up and running.");
			FTPServer.setDebug(true);
			
			/***** Added code lines *****/
			int numberOfCores = Runtime.getRuntime().availableProcessors();
			double blockingCoefficient = 0.9;
			int poolSize = (int) (numberOfCores / (1 - blockingCoefficient));
			ExecutorService executorPool = Executors.newFixedThreadPool(poolSize);
			/****************************/
			
			while (true)
			{
				FTPServer server = new FTPServer(socketaccueil.accept());
				//server.call();
				// TODO Modify for multithreading
				
				/***** Added code line *****/
				executorPool.submit(server);
				/***************************/
			}
			
		} finally
		{
			if (socketaccueil != null)
			{
				socketaccueil.close();
			}
		}
	}
}
